package cn.bnuz.edu.liyang.wait_eat_what;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.File;
import java.util.List;
import java.util.Map;

public class ListAdapter extends SimpleAdapter{
    private int resourceId;
    private List<Map<String,Object>> data;
    Things things = new Things();
    public  ListAdapter(Context context, List<Map<String, Object>> data, int resource){
        super(context,data,resource,null,null);
        this.resourceId = resource;
        this.data=data;
    }
    @NonNull
    @Override
    public View getView(int position,@Nullable View convertView,@NonNull ViewGroup parent){
        final int i = position;
        Things things =(Things)getItem(position);
        View view = null;
        ViewHolder viewHolder;
        if(convertView == null){
            view = LayoutInflater.from(view.getContext()).inflate(resourceId,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.thing_image = (ImageView)view.findViewById(R.id.thing_image);
            viewHolder.thing_name = (TextView) view.findViewById(R.id.thing_name);
            viewHolder.thing_from = (TextView) view.findViewById(R.id.thing_from);
            viewHolder.thing_price = (TextView) view.findViewById(R.id.thing_price);
            viewHolder.thing_stars = (TextView) view.findViewById(R.id.thing_stars);
            view.setTag(viewHolder);
        }
        else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.thing_image.setImageURI(Uri.fromFile(new File(things.getImg_url())));
        viewHolder.thing_name.setText(things.getName());
        viewHolder.thing_from.setText(things.getFrom());
        CharSequence cs = String.valueOf(things.getPrice());
        viewHolder.thing_price.setText(cs);
        cs = String.valueOf(things.getStars());
        viewHolder.thing_stars.setText(cs);
        return view;
    }
    class ViewHolder{
        ImageView thing_image;
        TextView thing_name;
        TextView thing_from;
        TextView thing_price;
        TextView thing_stars;
    }
}
